package com.css.modules.lawRepo.controller;

import com.css.common.utils.PageUtils;
import com.css.common.utils.R;
import com.css.common.validator.ValidatorUtils;
import com.css.common.validator.group.AddGroup;
import com.css.common.validator.group.UpdateGroup;
import com.css.modules.lawRepo.entity.LawRepoDocEntity;
import com.css.modules.lawRepo.service.LawRepoDocService;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author: Chen Ao
 * @Date: 2020/2/18/018
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping("lawRepo/doc")
@Api(value = "/doc", description = "法律法规文档库", tags = "法律法规文档库")
public class LawRepoDocController {

    @Autowired
    private LawRepoDocService lawRepoDocService;

    @GetMapping("/list")
    @ApiOperation(value = "法律法规列表", notes = "返回法律法规列表", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "sidx", value = "排序字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "order", value = "排序方式，如：asc、desc", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "lawName", value = "法律法规名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "lawCategory", value = "法律法规类别", dataType = "string", paramType = "query"),

    })
    //分页查询
    public R list(@ApiParam(name="params" ,hidden = true)@RequestParam Map<String, Object> param){

        PageUtils page = lawRepoDocService.queryPage(param);
        // test

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("lawRepo:lawRepoDoc:info")
    public R info(@PathVariable("id") String id){
        LawRepoDocEntity lawRepoDoc = lawRepoDocService.getById(id);
        return R.ok().put("lawRepoDoc",lawRepoDoc);
    }

    /**
     * 保存
     */

    @PostMapping("/save")
    @ApiOperation(value = "添加法律法规文档", produces = "application/json")
    public R save(@RequestBody LawRepoDocEntity lawRepoDoc){
        if(StringUtils.isNotBlank((lawRepoDoc.getId()))){
            ValidatorUtils.validateEntity(lawRepoDoc, AddGroup.class);
            lawRepoDoc.setId(null);
            lawRepoDoc.setDelFlag("0");
        }

        lawRepoDocService.save(lawRepoDoc);
        return R.ok();
    }


    /**
     * 修改
     */

    @ApiOperation(value = "修改法律法规文档", produces = "application/json")
    @PostMapping("/update")
 //   @RequiresPermissions("lawRepo:lawRepoDoc:info")
    public R update(@RequestBody LawRepoDocEntity lawRepoDoc){
        ValidatorUtils.validateEntity(lawRepoDoc, UpdateGroup.class);
        lawRepoDocService.updateById(lawRepoDoc);
        return R.ok();
    }


    /**
     * 全部删除
     */

    @GetMapping("/deleteAll")
    @ApiOperation(value = "全部删除", produces = "application/json")
    public R deleteAll(){
        lawRepoDocService.commonUpdateDelFlagAll("1");
        return R.ok();
    }

    /**
     * 批量删除
     */

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除", produces = "application/json")
    @ApiImplicitParam(name = "mapIds", value = "ids", paramType = "body",dataType = "body")
    public R deleteBatch(@RequestBody Map<String, Object> mapIds){
        lawRepoDocService.commonUpdateDelFlagBatch("1", mapIds);
        return R.ok();
    }





}
